﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DataTransfer;
namespace Gui
{
    public partial class frmThemBenhNhanKhamBenh : DevComponents.DotNetBar.Office2007Form
    {
        frmDanhSachKhamBenh frm_danhsachkhambenh;
        public frmThemBenhNhanKhamBenh(frmDanhSachKhamBenh frm,DSKB  info=null)
        {
            InitializeComponent();
            frm_danhsachkhambenh = frm;
            if (info != null)
            {
                txtMaBenhNhan.Text = info.ID.ToString();
                txtHoTen.Text = info.HoTen;
                txtDiaChi.Text = info.DiaChi;
                dtpNgayKham.Value = Convert.ToDateTime(info.NgayKham);
                dtpNgaySinh.Value = Convert.ToDateTime(info.NamSinh);
                rbNam.Checked = info.GioiTinh == 1 ? true : rbNu.Checked = true;
                ckDaKham.Checked = info.DaKham == 1 ? true : false;
                btnThem.Visible = false;

            }
            else
            {
                lbMaBenhNhan.Visible = false;
                txtMaBenhNhan.Visible = false;
                btnCapNhat.Visible = false;
            }
        }

        private DSKBBus dskb;
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
                            try 
	                {	        
		                   DSKB info = new DSKB();
                           info.HoTen = txtHoTen.Text;
                           info.GioiTinh =rbNam.Checked==true ?1:0;
                           info.NamSinh = dtpNgaySinh.Value.Date;
                           info.NgayKham = dtpNgayKham.Value.Date;
                           info.DiaChi = txtDiaChi.Text;
                           info.DaKham = ckDaKham.Checked == true ? 1 : 0;
                           info.UserCreate = frmMain.currentUser.UserName;
                           info.CreateDate = DateTime.Now;
                           dskb = new DSKBBus();
                          int id= dskb.Insert(info);
                          MessageBox.Show("Insert Success :ID=" + id);
	                }
	                catch (Exception ex)
	                {
                        MessageBox.Show(ex.Message);
		                
	                }
        } 

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                DSKB info = new DSKB();
                info.ID =Convert.ToInt32(txtMaBenhNhan.Text.ToString());
                info.HoTen = txtHoTen.Text.Trim();
                info.GioiTinh = rbNam.Checked == true ? 1 : 0;
                info.NamSinh = dtpNgaySinh.Value;
                info.NgayKham = dtpNgayKham.Value;
                info.DiaChi = txtDiaChi.Text.Trim();
                info.DaKham = ckDaKham.Checked == true ? 1 : 0;
                info.UserModify = frmMain.currentUser.UserName;
                info.ModifyDate = DateTime.Now;
                dskb = new DSKBBus();
                bool result = dskb.Update(info);
                MessageBox.Show("Update Success" );
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

        private void frmThemBenhNhanKhamBenh_FormClosing(object sender, FormClosingEventArgs e)
        {
            frm_danhsachkhambenh.loadAll();
        }

      
    }
}
